package kingAI.calc.util;

/**
 * Contains various operations on arrays required by the AI's probability calculators. 
 * 
 * These operations include:
 * Multiple and single dimensional array copiers
 * Particular element finders
 * 
 * @author Equilibrium
 * @version 1.0
 */
public class ArrayOperations {

	/**
	 * Copies a 1 dimensional boolean array.
	 * 
	 * @param array Array to be copied.
	 * @return A copy of the given array.
	 */
	public static boolean[] copy(boolean[] array){
		boolean[] newArray = new boolean[array.length];
		for(int i=0;i<array.length;i++)
			newArray[i]=array[i];
		return newArray;
	}
	
	/**
	 * Copies a 2 dimensional integer array.
	 * 
	 * @param array Array to be copied.
	 * @return A copy of the given array.
	 */
	public static int[][] copy(int[][] array){
		int[][] newArray = new int[array.length][array[0].length];
		for(int i=0;i<array.length;i++)
			for(int j=0;j<array[0].length;j++)
				newArray[i][j]=array[i][j];
		return newArray;
	}
	
	/**
	 * Finds the value of the middle element of the given 3-element double array.
	 * 
	 * @param array 3-element array.
	 * @return Value of the middle element of the array.
	 */
	public static double midElementOf(double[] array){
		if((array[0]<=array[1] && array[0]>=array[2]) || (array[0]>=array[1] && array[0]<=array[2]))
			return array[0];
		if((array[1]<=array[2] && array[1]>=array[0]) || (array[1]>=array[2] && array[1]<=array[0]))
			return array[1];
		if((array[2]<=array[1] && array[2]>=array[0]) || (array[2]>=array[1] && array[2]<=array[0]))
			return array[2];
		return 0;
	}
}
